package LeetCode;

public class LC_111_MinimumDepthofBinaryTree {

    public static void main(String[] args) {

    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        private int min = Integer.MAX_VALUE;
        private int cur = 0;

        public int minDepth(TreeNode root) {
            depth(root);
            return min;
        }

        private void depth(TreeNode node) {
            if (node == null) {
                min = cur;
                return;
            }
            cur++;
            if (node.left == null && node.right == null && cur < min) min = cur;
            if (node.left != null) depth(node.left);
            if (node.right != null) depth(node.right);
            cur--;
        }

    }

}